<?php
/*
Here's a funtion i used to resize and save images uploaded by the user, you can either create thumbnails or other images.
Main "feature" is that the width and height stay relativ to each other.

imgcomp is the quality, i turned it around so now its from 0 -best to 100 -most compressed.

For gif version just change the functions names.
*/

function resampimagejpg($forcedwidth, $forcedheight, $sourcefile, $destfile, $imgcomp)
{
	$g_imgcomp=100-$imgcomp;
	$g_srcfile=$sourcefile;
	$g_dstfile=$destfile;
	$g_fw=$forcedwidth;
	$g_fh=$forcedheight;

	if(file_exists($g_srcfile))
	{
		$g_is=getimagesize($g_srcfile);
		if(($g_is[0]-$g_fw)>=($g_is[1]-$g_fh))
		{
			$g_iw=$g_fw;
			$g_ih=($g_fw/$g_is[0])*$g_is[1];
		}
		else
		{
			$g_ih=$g_fh;
			$g_iw=($g_ih/$g_is[1])*$g_is[0];
		}
		$img_src=imagecreatefromjpeg($g_srcfile);
		
		
		//$img_dst=imagecreate($g_iw,$g_ih);
		$img_dst=imagecreatetruecolor($g_iw,$g_ih);
		
		
		imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $g_iw, $g_ih, $g_is[0], $g_is[1]);
		imagejpeg($img_dst, $g_dstfile, $g_imgcomp);
		imagedestroy($img_dst);
		return true;
	}
	else
	return false;
}


//300, 400  na quality 20  - thumbnail
//900, 1200 na quality 40  - image
//resampimagejpg(900, 1200, "../images/DSC00096.JPG", "../images/testing.JPG", 40);

?>